﻿@{
    ViewBag.Title = "渠道管理";
    Layout = "~/Views/Shared/_Index.cshtml";
 }
<section class="content">
    <div class="row">
         <div class="col-xs-12">
              <div class="box box-info">
                   <div class="box-body">
                        <div class="table-responsive">
                            <div class="btn-group-sm" id="toolbar" role="group"> 
                                <a id="btnAdd" authorize="yes" class="btn btn-success" onclick="showForm('','')"><i class="fa fa-plus"></i> 新增渠道</a>
                                <div class="col-sm-3 pull-right">
                                    <div class="input-group input-group-sm">
                                        <input type="text" id="keyWord" col="KeyWord" class="form-control" placeholder="请输入关键字查询" />
                                        <div class="input-group-btn">
                                            <button type="button" class="btn btn-primary" onclick="searchGrid()"><i class="fa fa-search"></i></button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                             <table id="gridTable" class="table table-hover text-nowrap">
                             </table>
                        </div>
                   </div>
              </div>
         </div>
    </div>
</section>
<script src="~/Content/plugin/bootstrap.table/1.12.0/bootstrap-table-merge-cells.js"></script>
<script type="text/javascript">
    var currentRowId = undefined;
    $(function () {
        initGrid();
    });

    function initGrid() {
        $('#gridTable').frameTable({
            url: '@Url.Action("GetPageListJson", "Channel")',
            striped: false,
            columns: [
                {
                    field: 'Name', title: '渠道名称', halign: "center", align: "center",
                    formatter: function (value, row, index) {
                        return '<a style="vertical-align: middle" class="text-blue text-bold" href="javascript:void(0);" onclick="delChannel(\'' + row.Id + '\')"><i class="fa fa-minus-circle fa-lg text-red"></i> ' + value + '</a>'
                    }
                },
                {
                    field: 'Address', title: '详细地址', halign: "center", align: "center",
                    formatter: function (value, row, index) {
                        return row.ProvinceCN + row.CityCN + row.AreaCN + row.Address;
                    }
                },
                { field: 'MangerName', title: '渠道经理', halign: "center", align: "center" },
                { field: 'MangerPhone', title: '联系方式', halign: "center", align: "center" },
                {
                    title: '操作',
                    halign: "center",
                    align: 'left',
                    formatter: function (value, row, index) {
                        var actions = []; 
                        if (index == 0 || (index !== 0 && !!currentRowId && row.Id != currentRowId)) {
                            actions.push('<a href="javascript:void(0);" authorize="yes" id="btnAddManager" class="btn btn-warning btn-xs" onclick="showForm(\'' + row.Id + '\',\'\')"><i class="fa fa-edit"></i> 新增</a> ');
                        }
                        actions.push('<a href="javascript:void(0);" authorize="yes" id="btnEdit" class="btn btn-primary btn-xs" onclick="showForm(\'' + row.Id + '\',\'' + row.ChannelManagerId + '\')"><i class="fa fa-edit"></i> 编辑</a> ');
                        actions.push('<a href="javascript:void(0);" authorize="yes" id="btnDelete" class="btn btn-danger btn-xs" onclick="delChannelManager(\'' + row.ChannelManagerId + '\')"><i class="fa fa-trash-o"></i> 删除</a>');
                        currentRowId = row.Id;
                        return actions.join('');
                    }
                }
            ],
            queryParams: function (params) {
                var pagination = $('#gridTable').frameTable('getPagination', params);
                var queryString = $('#toolbar').getWebControls(pagination);
                return queryString;
            },
            onLoadSuccess: function () {
                var cellFileds = new Array("Name","Address");
                $('#gridTable').mergeCells(cellFileds);
            },
            onPageChange: function () {
                var cellFileds = new Array("Name","Address");
                $('#gridTable').mergeCells(cellFileds);
            },
        });
    }

    function showForm(id, managerId) {
        frame.openDialog({
            title: !!id ? '编辑渠道' : '新增渠道',
            content: '@Url.Content("/Sale/Channel/Form")' + '?id=' + id + "&managerId=" + managerId,
            width: "500px",
            height: "450px",
            callback: function (index, layero) {
                var iframeWin = window[layero.find('iframe')[0]['name']];
                iframeWin.saveForm(index);
            }
        });
    }

    function searchGrid() {
        $('#gridTable').frameTable('search');
    }

    function delChannel(id) {
        frame.confirm("确定要删除选中的渠道吗？", function () {
            frame.ajax({
                url: '@Url.Content("/Sale/Channel/DeleteChannel")' + '?id=' + id,
                type: "post",
                success: function (obj) {
                    if (obj.success) {
                        frame.msgSuccess(obj.message);
                        searchGrid();
                    }
                    else {
                        frame.msgError(obj.message);
                    }
                }
            });
        });
    }

    function delChannelManager(id) {
        frame.confirm("确定要删除选中的渠道经理吗？", function () {
            frame.ajax({
                url: '@Url.Content("/Sale/Channel/DeleteManager")' + '?id=' + id,
                type: "post",
                success: function (obj) {
                    if (obj.success) {
                        frame.msgSuccess(obj.message);
                        searchGrid();
                    }
                    else {
                        frame.msgError(obj.message);
                    }
                }
            });
        });
    }
</script>
